//
//  HotViewController.m
//  VideoPic
//
//  Created by apple on 16/3/21.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "HotViewController.h"
#import "YSLDraggableCardContainer.h"
#import "YMCardViewForVideo.h"
#import "NetworkingHandle.h"
#import "YMModelForHotVideo.h"


#define RGB(r, g, b)	 [UIColor colorWithRed: (r) / 255.0 green: (g) / 255.0 blue: (b) / 255.0 alpha : 1]


@interface HotViewController ()<YSLDraggableCardContainerDelegate, YSLDraggableCardContainerDataSource>
{
    //记录刷新次数
    NSInteger refresh;
}

@property (nonatomic, strong) YSLDraggableCardContainer *container;
@property (nonatomic, strong) NSMutableArray *datas;

@end

@implementation HotViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor = RGB(235, 235, 235);
    
    _container = [[YSLDraggableCardContainer alloc]init];
    _container.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    _container.backgroundColor = [UIColor clearColor];
    _container.dataSource = self;
    _container.delegate = self;
    //设置可以拖动的方向
    _container.canDraggableDirection = YSLDraggableDirectionLeft | YSLDraggableDirectionRight | YSLDraggableDirectionUp | YSLDraggableDirectionDown;
    [self.view addSubview:_container];
    
    refresh = 1;
    [self loadData];
    
    
}

- (void)loadData
{
    NSString *url = [NSString stringWithFormat:@"http://api.miaopai.com/m/ascent_channel.json?os=ios&page=%ld&per=20&unique_id=af807d3462a5725ecc4cf34db619dc6e3856450562&version=6.3.1", refresh];
    
    [NetworkingHandle handleWorkData:url AndBody:nil value:nil handleBlock:^(id result) {
        self.datas = [NSMutableArray array];
        NSArray *tempArr = [result objectForKey:@"result"];
        if (tempArr.count > 0) refresh++;
        else {
            refresh = 1;
            [self loadData];
        }
        for (NSDictionary *tempDic in tempArr) {
            NSDictionary *dic = [tempDic objectForKey:@"channel"];
            if(![[dic objectForKey:@"type"] isEqual:@17]){
                YMModelForHotVideo *model = [[YMModelForHotVideo alloc] init];
                model.videoID = [dic objectForKey:@"scid"];
                [model setValuesForKeysWithDictionary:dic];
                [self.datas addObject:model];
            }
        }
        [self.container reloadCardContainer];
    }];
    
    
}

#pragma mark -- YSLDraggableCardContainer DataSource
- (UIView *)cardContainerViewNextViewWithIndex:(NSInteger)index
{
    //获取数据
    YMModelForHotVideo *model = self.datas[index];
    //创建card
    YMCardViewForVideo *view = [[YMCardViewForVideo alloc]initWithFrame:CGRectMake(20, 64 + 30, self.view.frame.size.width - 40, self.view.frame.size.height - 94 - 49 - 60)];
    view.backgroundColor = [UIColor whiteColor];
    //给里面的每个card传值
    [view passModel:model];
    
    return view;
}

- (NSInteger)cardContainerViewNumberOfViewInIndex:(NSInteger)index
{
    //设置card数量
    return _datas.count;
}

#pragma mark -- YSLDraggableCardContainer Delegate
- (void)cardContainerView:(YSLDraggableCardContainer *)cardContainerView didEndDraggingAtIndex:(NSInteger)index draggableView:(UIView *)draggableView draggableDirection:(YSLDraggableDirection)draggableDirection
{
    if (draggableDirection == YSLDraggableDirectionLeft) {
        [cardContainerView movePositionWithDirection:draggableDirection
                                         isAutomatic:NO];
    }
    
    if (draggableDirection == YSLDraggableDirectionRight) {
        [cardContainerView movePositionWithDirection:draggableDirection
                                         isAutomatic:NO];
    }
    
    if (draggableDirection == YSLDraggableDirectionUp) {
        [cardContainerView movePositionWithDirection:draggableDirection
                                         isAutomatic:NO];
    }
    
    if (draggableDirection == YSLDraggableDirectionDown) {
        [cardContainerView movePositionWithDirection:draggableDirection
                                         isAutomatic:NO];
    }
}

- (void)cardContainderView:(YSLDraggableCardContainer *)cardContainderView updatePositionWithDraggableView:(UIView *)draggableView draggableDirection:(YSLDraggableDirection)draggableDirection widthRatio:(CGFloat)widthRatio heightRatio:(CGFloat)heightRatio
{
    //设置拖动改变
    
}

- (void)cardContainerViewDidCompleteAll:(YSLDraggableCardContainer *)container;
{
    //数据为空后重新加载数据
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.3 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
        [self loadData];
    });
}

- (void)cardContainerView:(YSLDraggableCardContainer *)cardContainerView didSelectAtIndex:(NSInteger)index draggableView:(UIView *)draggableView
{
    //点击数据后
//    NSLog(@"++ index : %ld",(long)index);
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
